package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.StrategyCatalog;
import cn.wolfcode.luowowo.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.repository.StrategyCatalogRepository;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.util.DBHelper;
import cn.wolfcode.luowowo.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService{

    @Autowired
    private StrategyCatalogRepository strategyCatalogRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IDestinationService destinationService;

    @Override
    public StrategyCatalog get(String id) {
        return strategyCatalogRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyCatalog> list() {
        return strategyCatalogRepository.findAll();
    }

    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        strategyCatalogRepository.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {
        strategyCatalogRepository.deleteById(id);
    }

    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {
        //条件
        Query query = new Query();
        //获取query
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        return DBHelper.query(mongoTemplate,StrategyCatalog.class,pageable,query);
    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        //保存和更新,
        //更新是全量的,需要先查,后替换,然后更新
        //判断是否有id
        //保存前端传过来的id,通过id获取

        if(StringUtils.hasLength(strategyCatalog.getDestId())){
            //通过目的地id获取目的地
            Destination destination = destinationService.get(strategyCatalog.getDestId());
            //将目的地名称保存到攻略分类
            strategyCatalog.setDestName(destination.getName());
        }
        if(StringUtils.hasLength(strategyCatalog.getId())){
            //有id就更新
            this.update(strategyCatalog);
        }else{
            this.save(strategyCatalog);
        }
    }

    @Override
    public List<CatalogVO> groupList() {
        //创建集合
        List<CatalogVO> list = new ArrayList<>();
        //创建聚合管道
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,
                Aggregation.group("destName").
                        push("name").as("names").
                        push("id").as("ids")
        );
        //模板获取
        AggregationResults<Map> result = mongoTemplate.aggregate(agg,Map.class);
        //获取映射结果
        List<Map> datas = result.getMappedResults();
        //遍历
        for (Map data : datas) {
            //创建对象
            CatalogVO vo = new CatalogVO();
            //获取id的字符串放到destName里面
            vo.setDestName(data.get("_id").toString());
            //创建新的集合
            List<Map<String, Object>> mm = new ArrayList<>();
            //强制转换成list
            List<Object> names = (List<Object>) data.get("names");
            //强制转换成list
            List<Object> ids = (List<Object>) data.get("ids");
            //遍历name的长度
            for (int i = 0;i < names.size(); i++) {
                //创建map集合
                Map<String, Object> vv = new HashMap<>();
                //存入map
                String name = names.get(i).toString();
                //存入name
                String id = ids.get(i).toString();
                //存放id
                vv.put("id", id);
                //存放name
                vv.put("name", name);
                //添加到mm集合中
                mm.add(vv);
            }
            //设置进字段里面
            vo.setMapList(mm);
            //添加
            list.add(vo);
        }
        System.out.println(list);
        return list;
    }
}

